﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;

namespace SZ
{
    class DrawClipEnvelopTool : BaseTool
    {
        private MainFrm mainFrm = null;
        private AxMapControl mapControl = null;
        private RasterClipFrm rasterClipFrm = null;
        private IPoint pPoint = null;
        private bool isMouseDown = false;
        private INewEnvelopeFeedback pNewEnvelopeFeedback = null;
        private GeometryManageClass geometryManageClass = new GeometryManageClass();
        private ElementManageClass elementManageClass = new ElementManageClass();

        //构造函数
        public DrawClipEnvelopTool(MainFrm m_mainFrm, AxMapControl m_mapControl, RasterClipFrm m_rasterClipFrm)
        {
            mainFrm = m_mainFrm;
            mapControl = m_mapControl;
            rasterClipFrm = m_rasterClipFrm;
        }

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            if (Button == 1)
            {
                try
                {
                    if (this.mapControl.ActiveView == null)
                        return;
                    IActiveView activeView = this.mapControl.Map as IActiveView;
                    pPoint = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                    isMouseDown = true;
                }
                catch
                { 

                }
            }
        }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            if (Button == 1)
            {
                if (!isMouseDown) return;
                IActiveView pActiveView = this.mapControl.Map as IActiveView;
                if (pNewEnvelopeFeedback == null)
                {
                    pNewEnvelopeFeedback = new NewEnvelopeFeedbackClass();
                    pNewEnvelopeFeedback.Display = pActiveView.ScreenDisplay;
                    pNewEnvelopeFeedback.Start(pPoint);
                }
                pNewEnvelopeFeedback.MoveTo(pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y));

            }
        }

        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            if (Button == 1)
            {
                if (!isMouseDown || pNewEnvelopeFeedback == null)
                    return;
                IActiveView pActiveView = this.mapControl.Map as IActiveView;
                IEnvelope pEnvelop = pNewEnvelopeFeedback.Stop();
                if (pEnvelop == null || pEnvelop.IsEmpty)
                {
                    pNewEnvelopeFeedback = null;
                    isMouseDown = false;
                    return;
                }

                this.rasterClipFrm.upText.Text = pEnvelop.YMax.ToString();
                this.rasterClipFrm.downText.Text = pEnvelop.YMin.ToString();
                this.rasterClipFrm.leftText.Text = pEnvelop.XMin.ToString();
                this.rasterClipFrm.rightText.Text = pEnvelop.XMax.ToString();

                IGeometry pRectangle = geometryManageClass.CreateRectangle(pEnvelop);
                IRgbColor pRgbColor = elementManageClass.GetColor(0, 255, 255);
                IElement pElement = elementManageClass.CreateElement(pRectangle, pRgbColor, pRgbColor);

                elementManageClass.AddElement(this.mapControl.Map, pElement, PublicVariablesClass.ClipRasterLayerName);
                pNewEnvelopeFeedback = null;
                isMouseDown = false;

            }
        }

        public override void OnCreate(object hook)
        {
            //base.m_cursor = new System.Windows.Forms.Cursor(GetType(), "Resources.Tool1.cur");
            //base.m_cursor  = new System.Windows.Forms.Cursor(GetType(), "Resources.Tools.cur");
        }

        public override void OnClick()
        {

        }
    }
}
